package com.ruoyi.canteen.controller;

import java.util.List;

import com.ruoyi.canteen.domain.CanteenMd;
import com.ruoyi.canteen.service.ICanteenMdService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.canteen.domain.CanteenCpOrder;
import com.ruoyi.canteen.service.ICanteenCpOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 主订单Controller
 * 
 * @author zhangw
 * @date 2021-09-19
 */
@RestController
@RequestMapping("/canteen/order")
public class CanteenCpOrderController extends BaseController
{
    @Autowired
    private ICanteenCpOrderService canteenCpOrderService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ICanteenMdService canteenMdService;

    /**
     * 查询主订单列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(CanteenCpOrder canteenCpOrder)
    {
        startPage();
        List<CanteenCpOrder> list = canteenCpOrderService.selectCanteenCpOrderList(canteenCpOrder);
        return getDataTable(list);
    }

    /*
      fwq0930
    * */
    //@PreAuthorize("@ss.hasPermi('canteen:order:list')")
    @GetMapping("/list1to1")
    public TableDataInfo list1to1(CanteenCpOrder canteenCpOrder)
    {
        //startPage();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        //获取登陆者所属门店id
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CanteenMd md = null;
        if(loginUser.getDeptId() != null)
        {

            md =  canteenMdService.selectCanteenMdByDeptId(loginUser.getDeptId());
            if(md != null)
                canteenCpOrder.setMdId(md.getMdId());
        }

        List<CanteenCpOrder> list = canteenCpOrderService.selectCanteenCpOrderList1to1(canteenCpOrder,pageDomain);
        return getDataTable(list);
    }

    /**
     * 导出主订单列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:export')")
    @Log(title = "主订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CanteenCpOrder canteenCpOrder)
    {
        List<CanteenCpOrder> list = canteenCpOrderService.selectCanteenCpOrderList(canteenCpOrder);
        ExcelUtil<CanteenCpOrder> util = new ExcelUtil<CanteenCpOrder>(CanteenCpOrder.class);
        return util.exportExcel(list, "主订单数据");
    }

    /**
     * 获取主订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(canteenCpOrderService.selectCanteenCpOrderById(id));
    }

    /**
     * 新增主订单
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:add')")
    @Log(title = "主订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CanteenCpOrder canteenCpOrder)
    {
        return toAjax(canteenCpOrderService.insertCanteenCpOrder(canteenCpOrder));
    }

    /**
     * 修改主订单
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:edit')")
    @Log(title = "主订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CanteenCpOrder canteenCpOrder)
    {
        return toAjax(canteenCpOrderService.updateCanteenCpOrder(canteenCpOrder));
    }

    /**
     * 删除主订单
     */
    @PreAuthorize("@ss.hasPermi('canteen:order:remove')")
    @Log(title = "主订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(canteenCpOrderService.deleteCanteenCpOrderByIds(ids));
    }

}
